using SM.core;
using System;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace SS
{
	public class frmLogin : Form
	{
		private IContainer components = null;
		private TextBox txtPassWord;
		private TextBox txtUserName;
		private Label label1;
		private Label label2;
		private Label lblMsg;
		private Button cmdClose;
		private Button cmdLogin;
		private Label label3;
		private Label label4;
		private Label label5;
		private Func objFun = new Func();
		public string UserName;
		public string FullName;
		public string Password;
		public bool OK = false;
		private Account objAccount = new Account();
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(frmLogin));
			this.txtPassWord = new TextBox();
			this.txtUserName = new TextBox();
			this.label1 = new Label();
			this.label2 = new Label();
			this.lblMsg = new Label();
			this.cmdClose = new Button();
			this.cmdLogin = new Button();
			this.label3 = new Label();
			this.label4 = new Label();
			this.label5 = new Label();
			base.SuspendLayout();
			this.txtPassWord.Location = new System.Drawing.Point(201, 127);
			this.txtPassWord.Name = "txtPassWord";
			this.txtPassWord.PasswordChar = '*';
			this.txtPassWord.Size = new Size(184, 21);
			this.txtPassWord.TabIndex = 13;
			this.txtPassWord.Leave += new EventHandler(this.txtPassword_Leave);
			this.txtPassWord.KeyPress += new KeyPressEventHandler(this.txtPassword_KeyPress);
			this.txtPassWord.Enter += new EventHandler(this.txtPassword_Enter);
			this.txtUserName.Location = new System.Drawing.Point(201, 100);
			this.txtUserName.Name = "txtUserName";
			this.txtUserName.Size = new Size(184, 21);
			this.txtUserName.TabIndex = 12;
			this.txtUserName.Leave += new EventHandler(this.txtUserName_Leave);
			this.txtUserName.KeyPress += new KeyPressEventHandler(this.txtUserName_KeyPress);
			this.txtUserName.Enter += new EventHandler(this.txtUserName_Enter);
			this.label1.AutoSize = true;
			this.label1.BackColor = Color.Transparent;
			this.label1.Location = new System.Drawing.Point(102, 103);
			this.label1.Name = "label1";
			this.label1.Size = new Size(96, 15);
			this.label1.TabIndex = 14;
			this.label1.Text = "Tên đăng nhập :";
			this.label2.AutoSize = true;
			this.label2.BackColor = Color.Transparent;
			this.label2.Location = new System.Drawing.Point(136, 130);
			this.label2.Name = "label2";
			this.label2.Size = new Size(62, 15);
			this.label2.TabIndex = 15;
			this.label2.Text = "Mật khẩu :";
			this.lblMsg.BackColor = Color.Transparent;
			this.lblMsg.ForeColor = Color.Red;
//			this.lblMsg.Image = (Image)componentResourceManager.GetObject("lblMsg.Image");
			this.lblMsg.ImageAlign = ContentAlignment.MiddleLeft;
			this.lblMsg.Location = new System.Drawing.Point(54, 185);
			this.lblMsg.Name = "lblMsg";
			this.lblMsg.Size = new Size(350, 21);
			this.lblMsg.TabIndex = 18;
			this.lblMsg.TextAlign = ContentAlignment.MiddleLeft;
			this.lblMsg.Visible = false;
			this.cmdClose.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.cmdClose.BackColor = Color.FromArgb(192, 255, 255);
			this.cmdClose.FlatAppearance.BorderColor = Color.FromArgb(0, 192, 192);
			this.cmdClose.FlatStyle = FlatStyle.System;
			this.cmdClose.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
//			this.cmdClose.Image = (Image)componentResourceManager.GetObject("cmdClose.Image");
			this.cmdClose.ImageAlign = ContentAlignment.MiddleLeft;
//			this.cmdClose.ImeMode = ImeMode.NoControl;
			this.cmdClose.Location = new System.Drawing.Point(325, 221);
			this.cmdClose.Name = "cmdClose";
			this.cmdClose.Size = new Size(90, 30);
			this.cmdClose.TabIndex = 27;
			this.cmdClose.Text = "Thoát [ ESC ]";
			this.cmdClose.UseVisualStyleBackColor = false;
			this.cmdClose.Click += new EventHandler(this.cmdClose_Click);
			this.cmdLogin.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.cmdLogin.BackColor = Color.FromArgb(192, 255, 255);
			this.cmdLogin.FlatAppearance.BorderColor = Color.FromArgb(0, 192, 192);
			this.cmdLogin.FlatStyle = FlatStyle.System;
			this.cmdLogin.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
            //this.cmdLogin.Image = (Image)componentResourceManager.GetObject("cmdLogin.Image");
			this.cmdLogin.ImageAlign = ContentAlignment.MiddleLeft;
//			this.cmdLogin.ImeMode = ImeMode.NoControl;
			this.cmdLogin.Location = new System.Drawing.Point(232, 221);
			this.cmdLogin.Name = "cmdLogin";
			this.cmdLogin.Size = new Size(90, 30);
			this.cmdLogin.TabIndex = 26;
			this.cmdLogin.Text = "Đăng nhập";
			this.cmdLogin.UseVisualStyleBackColor = false;
			this.cmdLogin.Click += new EventHandler(this.cmdLogin_Click);
			this.label3.AutoSize = true;
			this.label3.BackColor = Color.Transparent;
			this.label3.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 0);
			this.label3.ForeColor = Color.Blue;
			this.label3.Location = new System.Drawing.Point(17, 16);
			this.label3.Name = "label3";
			this.label3.Size = new Size(287, 15);
			this.label3.TabIndex = 28;
			this.label3.Text = "ĐĂNG NHẬP VÀO HỆ THỐNG QUẢN LÝ NHÀ THUỐC";
			this.label4.AutoSize = true;
			this.label4.BackColor = Color.Transparent;
			this.label4.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.label4.ForeColor = Color.Black;
			this.label4.Location = new System.Drawing.Point(18, 35);
			this.label4.Name = "label4";
			this.label4.Size = new Size(273, 15);
			this.label4.TabIndex = 29;
			this.label4.Text = "Nhập vào tên đăng nhập và mật khẩu có hiệu lực";
			this.label5.AutoSize = true;
			this.label5.BackColor = Color.Transparent;
			this.label5.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 163);
			this.label5.ForeColor = Color.FromArgb(0, 0, 192);
			this.label5.Location = new System.Drawing.Point(143, 155);
			this.label5.Name = "label5";
			this.label5.Size = new Size(241, 15);
			this.label5.TabIndex = 30;
			this.label5.Text = "Tên đăng nhập : admin    -    Mật khẩu : 123";
			this.label5.Visible = false;
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
            //this.BackgroundImage = (Image)componentResourceManager.GetObject("$this.BackgroundImage");
			base.ClientSize = new Size(422, 257);
			base.Controls.Add(this.label5);
			base.Controls.Add(this.label4);
			base.Controls.Add(this.label3);
			base.Controls.Add(this.cmdClose);
			base.Controls.Add(this.cmdLogin);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.label1);
			base.Controls.Add(this.txtPassWord);
			base.Controls.Add(this.txtUserName);
			base.Controls.Add(this.lblMsg);
			this.Cursor = Cursors.Hand;
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			base.MaximizeBox = false;
			this.MaximumSize = new Size(438, 291);
			base.MinimizeBox = false;
			this.MinimumSize = new Size(438, 291);
			base.Name = "frmLogin";
			base.ShowIcon = false;
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = "  .: Đăng nhập hệ thống";
			base.TopMost = true;
			base.Load += new EventHandler(this.frmLogin_Load);
			base.KeyDown += new KeyEventHandler(this.frmLogin_KeyDown);
			base.ResumeLayout(false);
			base.PerformLayout();
		}
		public frmLogin()
		{
			this.InitializeComponent();
		}
		private void cmdClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		private void cmdLogin_Click(object sender, EventArgs e)
		{
			this.lblMsg.Text = "";
			this.lblMsg.Visible = false;
			if (this.txtUserName.Text.Trim() == "" || this.txtPassWord.Text.Trim() == "")
			{
				this.lblMsg.Visible = true;
				this.lblMsg.Text = "        Bạn phải nhập vào đầy đủ thông tin!";
				if (this.txtUserName.Text == "")
				{
					this.txtUserName.Focus();
				}
				else
				{
					this.txtPassWord.Focus();
				}
			}
			else
			{
				if (!this.objAccount.CheckStatus(this.txtUserName.Text))
				{
					MessageBox.Show("Tài khoản hiện tại đang bị khoá \r\n\r\nBạn không thể đăng nhập hệ thống với tài khoản này !");
				}
				else
				{
					bool flag = false;
					try
					{
						SqlConnection sqlConnection = new SqlConnection(this.objFun.strConnectionString);
						sqlConnection.Open();
						SqlCommand sqlCommand = sqlConnection.CreateCommand();
						sqlCommand.CommandText = "SELECT [FullName], [UserName], [Pass] FROM [TabUser] WHERE [UserName] = '" + this.txtUserName.Text.Replace("--", "").Replace("'", "") + "'";
						SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
						while (sqlDataReader.Read() && !flag)
						{
							if (this.objFun.CryptographyMD5(this.txtPassWord.Text.Trim()) == sqlDataReader["Pass"].ToString())
							{
								this.FullName = sqlDataReader["FullName"].ToString();
								this.UserName = sqlDataReader["UserName"].ToString();
								flag = true;
							}
						}
						sqlDataReader.Close();
						sqlDataReader.Dispose();
						sqlConnection.Close();
						sqlConnection.Dispose();
					}
					catch
					{
					}
					if (!flag)
					{
						this.lblMsg.Visible = true;
						this.lblMsg.Text = "        Thông tin đăng nhập không hợp lệ!";
					}
					else
					{
						this.Password = this.txtPassWord.Text.Trim();
						this.OK = true;
						base.Close();
					}
				}
			}
		}
		private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r' && this.txtPassWord.Text != "")
			{
				if (this.txtUserName.Text != "" && this.txtPassWord.Text != "")
				{
					this.cmdLogin.PerformClick();
				}
				else
				{
					if (this.txtUserName.Text == "")
					{
						this.txtUserName.Focus();
					}
				}
			}
		}
		private void txtUserName_Enter(object sender, EventArgs e)
		{
			this.txtUserName.BackColor = Color.Aqua;
		}
		private void txtUserName_Leave(object sender, EventArgs e)
		{
			this.txtUserName.BackColor = Color.White;
		}
		private void txtPassword_Leave(object sender, EventArgs e)
		{
			this.txtPassWord.BackColor = Color.White;
		}
		private void txtPassword_Enter(object sender, EventArgs e)
		{
			this.txtPassWord.BackColor = Color.Aqua;
		}
		private void frmLogin_Load(object sender, EventArgs e)
		{
			this.txtUserName.Focus();
		}
		private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				this.txtPassWord.Focus();
			}
		}
		private void frmLogin_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}
	}
}
